#include "common.h"

// 初始化队列
Queue* queue_init() {
    Queue* queue = (Queue*)malloc(sizeof(Queue));
    if (!queue) {
        HANDLE_ERROR("malloc queue failed");
    }
    
    queue->front = NULL;
    queue->rear = NULL;
    queue->size = 0;
    
    if (pthread_mutex_init(&queue->mutex, NULL) != 0) {
        free(queue);
        HANDLE_ERROR("mutex init failed");
    }
    
    if (pthread_cond_init(&queue->not_empty, NULL) != 0) {
        pthread_mutex_destroy(&queue->mutex);
        free(queue);
        HANDLE_ERROR("cond not_empty init failed");
    }
    
    if (pthread_cond_init(&queue->not_full, NULL) != 0) {
        pthread_cond_destroy(&queue->not_empty);
        pthread_mutex_destroy(&queue->mutex);
        free(queue);
        HANDLE_ERROR("cond not_full init failed");
    }
    
    return queue;
}

// 销毁队列
void queue_destroy(Queue* queue) {
    if (!queue) return;
    
    pthread_mutex_lock(&queue->mutex);
    
    // 释放队列中的所有节点
    QueueNode* current = queue->front;
    while (current) {
        QueueNode* next = current->next;
        free(current->data);
        free(current);
        current = next;
    }
    
    pthread_mutex_unlock(&queue->mutex);
    pthread_mutex_destroy(&queue->mutex);
    pthread_cond_destroy(&queue->not_empty);
    pthread_cond_destroy(&queue->not_full);
    
    free(queue);
}

// 入队操作
int queue_push(Queue* queue, void* data, size_t size) {
    if (!queue || !data) return -1;
    
    QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
    if (!node) return -1;
    
    // 为数据分配内存并复制
    node->data = malloc(size);
    if (!node->data) {
        free(node);
        return -1;
    }
    
    memcpy(node->data, data, size);
    node->size = size;
    node->next = NULL;
    
    pthread_mutex_lock(&queue->mutex);
    
    // 等待队列不满
    while (queue->size >= MAX_QUEUE_SIZE) {
        pthread_cond_wait(&queue->not_full, &queue->mutex);
    }
    
    // 将节点添加到队列尾部
    if (queue->rear) {
        queue->rear->next = node;
        queue->rear = node;
    } else {
        queue->front = queue->rear = node;
    }
    
    queue->size++;
    
    // 通知等待的消费者
    pthread_cond_signal(&queue->not_empty);
    pthread_mutex_unlock(&queue->mutex);
    
    return 0;
}

// 出队操作
void* queue_pop(Queue* queue, size_t* size) {
    if (!queue) return NULL;
    
    pthread_mutex_lock(&queue->mutex);
    
    // 等待队列不空
    while (queue->size == 0) {
        pthread_cond_wait(&queue->not_empty, &queue->mutex);
    }
    
    // 从队列头部取出节点
    QueueNode* node = queue->front;
    queue->front = node->next;
    
    if (!queue->front) {
        queue->rear = NULL;
    }
    
    queue->size--;
    
    // 通知等待的生产者
    pthread_cond_signal(&queue->not_full);
    pthread_mutex_unlock(&queue->mutex);
    
    // 提取数据
    void* data = node->data;
    if (size) *size = node->size;
    
    free(node);
    return data;
}

// 检查队列是否为空
int queue_is_empty(Queue* queue) {
    if (!queue) return 1;
    
    pthread_mutex_lock(&queue->mutex);
    int is_empty = (queue->size == 0);
    pthread_mutex_unlock(&queue->mutex);
    
    return is_empty;
}

// 检查队列是否已满
int queue_is_full(Queue* queue) {
    if (!queue) return 1;
    
    pthread_mutex_lock(&queue->mutex);
    int is_full = (queue->size >= MAX_QUEUE_SIZE);
    pthread_mutex_unlock(&queue->mutex);
    
    return is_full;
} 